package com.zxw.third.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxw.third.mapper.BizJobMapper;
import com.zxw.third.service.BizJobService;
import com.zxw.common.entity.bizjob.BizJobEntity;
import com.zxw.common.entity.bizjob.BizJobListQuery;
import com.zxw.common.entity.bizjob.BizJobQuery;
import com.zxw.common.entity.bizjob.BizJobVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhanxuewei
 * @since 2022-10-12
 */
@Service
public class BizJobServiceImpl extends ServiceImpl<BizJobMapper, BizJobEntity> implements BizJobService {


    /**
     * 添加
     *
     * @param bizJobQuery BizJobQuery
     * @return 是否成功 true|false
     */
    @Override
    public boolean save(BizJobQuery bizJobQuery) {
        BizJobEntity bizJobEntity = new BizJobEntity();
        BeanUtils.copyProperties(bizJobQuery, bizJobEntity);
        bizJobEntity.setId(null);
        bizJobEntity.setStatus("1");
        return super.save(bizJobEntity);
    }

    /**
     * 批量添加
     *
     * @param list List<BizJobQuery>
     * @return 是否成功 true|false
     */
    @Override
    public boolean saveBatch(List<BizJobQuery> list) {
        List<BizJobEntity> collect = list.stream().map(item -> {
            BizJobEntity bizJobEntity = new BizJobEntity();
            BeanUtils.copyProperties(item, bizJobEntity);
            bizJobEntity.setId(null);
            bizJobEntity.setStatus("1");
            return bizJobEntity;
        }).collect(Collectors.toList());
        return super.saveBatch(collect);
    }

    /**
     * 根据ID集合批量删除
     *
     * @param ids id集合
     * @return 是否成功 true|false
     */
    @Override
    public boolean removeByIds(List<String> ids) {
        return super.removeByIds(ids);
    }

    /**
     * 根据ID修改
     *
     * @param bizJobQuery BizJobQuery
     * @return 是否成功 true|false
     */
    @Override
    public boolean updateById(BizJobQuery bizJobQuery) {
        BizJobEntity bizJobEntity = new BizJobEntity();
        BeanUtils.copyProperties(bizJobQuery, bizJobEntity);
        return super.updateById(bizJobEntity);
    }

    /**
     * 根据ID查询数据
     *
     * @param id ID
     * @return BizJobDO
     */
    @Override
    public BizJobEntity getById(String id) {
        return super.getById(id);
    }

    /**
     * 条件分页查询
     *
     * @param bizJobListQuery BizJobListQuery
     * @return IPage<BizJobDO>
     */
    @Override
    public IPage<BizJobEntity> list(BizJobListQuery bizJobListQuery) {
        IPage<BizJobEntity> pageInfo = new Page<>(bizJobListQuery.getCurrent(), bizJobListQuery.getSize());
        QueryWrapper<BizJobEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BizJobEntity::getStatus, 1)
                .orderByDesc(BizJobEntity::getCreateTime);
        return super.page(pageInfo, queryWrapper);
    }

    @Override
    public IPage<BizJobVO> pageList(BizJobListQuery bizJobListQuery) {
        IPage<BizJobEntity> pageInfo = new Page<>(bizJobListQuery.getCurrent(), bizJobListQuery.getSize());
        QueryWrapper<BizJobEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<BizJobEntity> lambda = queryWrapper.lambda();

        if (StringUtils.isNotBlank(bizJobListQuery.getJobDesc())) {
            lambda.like(BizJobEntity::getJobDesc, bizJobListQuery.getJobDesc());
        }

        lambda.orderByDesc(BizJobEntity::getCreateTime);

        IPage<BizJobEntity> page = super.page(pageInfo, queryWrapper);
        return page.convert(bizJobEntity -> {
            BizJobVO bizJobVO = new BizJobVO();
            BeanUtils.copyProperties(bizJobEntity, bizJobVO);
            return bizJobVO;
        });
    }

}
